import tkinter as tk
from tkinter import messagebox


def print_board(board):
    for i in range(len(board)):
        if i % 3 == 0 and i!= 0:
            print("- - - - - - - - - - - - - ")
        for j in range(len(board[0])):
            if j % 3 == 0 and j!= 0:
                print("| ", end="")
            if j == 8:
                print(board[i][j])
            else:
                print(str(board[i][j]) + " ", end="")


def find_empty(board):
    for i in range(len(board)):
        for j in range(len(board[0])):
            if board[i][j] == 0:
                return (i, j)
    return None


def valid(board, num, pos):
    # 检查行
    for i in range(len(board[0])):
        if board[pos[0]][i] == num and pos[1]!= i:
            return False
    # 检查列
    for i in range(len(board)):
        if board[i][pos[1]] == num and pos[0]!= i:
            return False
    # 检查 3x3 小方格
    box_x = pos[1] // 3
    box_y = pos[0] // 3
    for i in range(box_y * 3, box_y * 3 + 3):
        for j in range(box_x * 3, box_x * 3 + 3):
            if board[i][j] == num and (i, j)!= pos:
                return False
    return True


def solve(board):
    find = find_empty(board)
    if not find:
        return True
    else:
        row, col = find
    for i in range(1, 10):
        if valid(board, i, (row, col)):
            board[row][col] = i
            if solve(board):
                return True
            board[row][col] = 0
    return False


def solve_button_click():
    board = []
    for i in range(9):
        row = []
        for j in range(9):
            value = int(cells[i][j].get())
            row.append(value)
        board.append(row)
    if solve(board):
        for i in range(9):
            for j in range(9):
                cells[i][j].set(board[i][j])
    else:
        messagebox.showinfo("Info", "No solution exists")


def clear_button_click():
    for i in range(9):
        for j in range(9):
            cells[i][j].set(0)


def main():
    global cells
    root = tk.Tk()
    root.title("Sudoku Solver")
    cells = []
    # 初始时隐藏部分内容
    initial_board = [
        [0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0]
    ]
    for i in range(9):
        row = []
        for j in range(9):
            cell = tk.StringVar()
            entry = tk.Entry(root, width=2, font=('Arial', 20), textvariable=cell)
            entry.insert(0, str(initial_board[i][j]))
            entry.grid(row=i, column=j)
            row.append(cell)
        cells.append(row)
    solve_button = tk.Button(root, text="Solve", command=solve_button_click)
    solve_button.grid(row=9, columnspan=9)
    clear_button = tk.Button(root, text="Clear", command=clear_button_click)
    clear_button.grid(row=10, columnspan=9)
    root.mainloop()


if __name__ == "__main__":
    main()